package com.qsl.workflow.server.controller;


import com.qsl.common.result.ResultEntity;
import com.qsl.sync.api.OfflineSyncApi;
import com.qsl.sync.common.dto.SyncTaskDTO;
import com.qsl.workflow.common.WorkflowDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 青石路
 */
@RestController
@RequestMapping("/definition")
public class WorkflowController {

    private static final Logger LOG = LoggerFactory.getLogger(WorkflowController.class);

    @Resource
    private OfflineSyncApi offlineSyncApi;

    @PostMapping("/delete")
    public ResultEntity<String> delete(@RequestBody WorkflowDTO workflow) {
        LOG.info("删除工作流[workflowId={}]", workflow.getWorkflowId());
        // 1.查询工作流节点，查到离线同步节点（taskId = 1）
        // 2.删除工作流节点，删除离线同步节点
        // 删除离线同步任务，isClearData直接传true
        ResultEntity<String> syncDeleteResult = offlineSyncApi.deleteTask(new SyncTaskDTO(1L, true));
        if (syncDeleteResult.getCode() != 200) {
            LOG.error("删除离线同步任务[taskId={}]失败：{}", 1, syncDeleteResult.getMessage());
            ResultEntity.fail(syncDeleteResult.getMessage());
        }
        return ResultEntity.success("删除成功");
    }
}
